package com.Services;

import com.Dao.UserDao;
import com.entity.User;
import com.entity.Userinfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2017/3/15.
 */
public class UserService {
    private UserDao ud;

    public UserDao getUd() {
        return ud;
    }

    public void setUd(UserDao ud) {
        this.ud = ud;
    }

    /**
     * 该功能用于用户登录并判别用户与管理员
     */
    public String login(String userNumber,String password){
        //先判别用户是否存在
        if(ud.searchAccount(userNumber)){
            String pass=ud.getPassword(userNumber);
            //再判别密码是否正确
            if(pass.trim().equals(password.trim())){
                int privilege=ud.getPrivilege(userNumber);
                //最后判别权限
                if(privilege==1){
                    return "user";
                }else {
                    return "admin";
                }
            }
        }
        //用户名或密码错误!
        return "error";
    }

    /**
     * 该功能用于注册用户
     */
    public String register(String Name,String Number,String password,String confirmPasswor,String superpassword,
                           String confirmSuperPassword, String sex, String year, String month, String cellphone,
                           String address, String education, String idCard){
        //先判断用户是否存在
        if(ud.searchAccount(Number)){
            return "user";
        }else{
            //再判断密码和超级密码两次都正确
            if(password.trim().equals(confirmPasswor.trim())){
                if(superpassword.trim().equals(confirmSuperPassword.trim())){
                    ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
                    User user=ac.getBean(User.class);
                    user.setPrivilege(1);
                    user.setPassword(password);
                    user.setSuperpassword(superpassword);
                    user.setAccount(Number);
                    Userinfo userinfo=ac.getBean(Userinfo.class);
                    userinfo.setAccount(Number);
                    userinfo.setAddress(address);
                    userinfo.setBirthday(year+"-"+month);
                    userinfo.setCellphone(cellphone);
                    userinfo.setEducation(education);
                    userinfo.setIdCard(idCard);
                    userinfo.setName(Name);
                    userinfo.setSex(sex);
                    ud.addUser(user,userinfo);
                    return "success";
                }else{
                    return "super";
                }
            }else{
                return "password";
            }
        }
    }
    /**
     * 该功能用于用户找回密码
     */
    public String findpassword(String account,String superpassword,String pass,String pass2){
        if(!ud.searchAccount(account)){
            return "user";
        }else{
            String superpass=ud.getSuperPassword(account);
            if(superpass.trim().equals(superpassword.trim())){
                if(pass.trim().equals(pass2.trim())){
                    ud.alterPassword(account,pass);
                    return "success";
                }else{
                    return "pass";
                }
            }
            else {
                return "super";
            }
        }
    }
}
